/**
 * @file qq_login.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_QQ != 0
/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
static void qq_create_qrcode_scan(lv_obj_t * par);
static void qq_show_qrcode_invalid(void);
static void qq_invalid_qrcode_refresh_event_cb(lv_obj_t * img, lv_event_t event);
static void qq_logout_confirm_event_cb(lv_obj_t * btnm, lv_event_t event);
static void qq_no_net_btn_event_cb(lv_obj_t * btn, lv_event_t event);
static void qq_scan_login_btn_event_cb(lv_obj_t * btn, lv_event_t event);
static void qq_login_btn_event_cb(lv_obj_t * btn, lv_event_t event);
static void qq_switch_account_event_cb(lv_obj_t * obj, lv_event_t event);
/**********************
 *  STATIC VARIABLES
 **********************/
extern char * my_access_token;
extern char * my_openid;
/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
void qq_destroy_login(void)
{
    lv_qq_obj_ext_t * ext = qq_get_ext();
    lv_obj_del(ext->login);
    ext->login = NULL;
}

void qq_show_login(void)
{
    lv_qq_obj_ext_t * ext = qq_get_ext();
    if(ext->qr_bg_cont) {
        lv_obj_del(ext->qr_bg_cont);
        ext->qr_bg_cont = NULL;
    }
    lv_obj_clean(ext->login);
    lv_img_cache_invalidate_src(QQ_QR_PATH);
    lv_img_cache_invalidate_src(ICON_QQ_SCAN_LOGIN);
    lv_img_set_src(ext->login, ICON_QQ_LOGINING);

    lv_obj_t * label = lv_label_create(ext->login, NULL);
    lv_label_set_text_id(label, WATCH_TEXT_ID_QQ_LOGGING_IN);
    lv_obj_set_style_local_text_color(label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
    lv_obj_align(label, NULL, LV_ALIGN_CENTER, 0, LV_VER_RES / 3);

#if QQ_UI_TEST
    Hal_Timer_Start(1000, qq_login_img_destroyed_timer_cb, NULL, false);
#endif
}

void qq_login_failed_timer_cb(lv_anim_t * anim)
{
    lv_obj_clean((lv_obj_t *)anim->var);
    qq_create_scan_login();
}

void qq_show_login_failed(void)
{
    lv_qq_obj_ext_t * ext = qq_get_ext();
    lv_obj_t * label = lv_obj_get_child(ext->login, NULL);
    lv_label_set_text_id(label, WATCH_TEXT_ID_QQ_LOGIN_FAILED);
    lv_obj_set_style_local_text_color(label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED);
    lv_obj_realign(label);
    lv_anim_t a = {0};
    lv_anim_init(&a);
    lv_anim_set_var(&a, ext->login);
    lv_anim_set_ready_cb(&a, (lv_anim_ready_cb_t)qq_login_failed_timer_cb);
    lv_anim_set_repeat_count(&a, 1);
    lv_anim_set_time(&a, 2000);
    lv_anim_start(&a);
}

void qq_show_qrcode(void * src)
{
    lv_qq_obj_ext_t * ext = qq_get_ext();
    lv_obj_t * cont = ext->qr_bg_cont;
    lv_obj_t * qr_bg = lv_obj_get_child(cont, lv_obj_get_child(cont, NULL));
    lv_obj_del(lv_obj_get_child(qr_bg, NULL));
    lv_obj_t * img = lv_img_create(qr_bg, NULL);
    lv_img_cache_invalidate_src(src);
    lv_img_set_src(img, src);
    lv_obj_align(img, NULL, LV_ALIGN_CENTER, 0, 0);
#if QQ_UI_TEST == 0
    watch_qq_query_qr_req();
#endif
}

void qq_query_qrcode(void * param)
{
#if QQ_UI_TEST == 0
    watch_qq_query_qr_req();
#endif
}

void qq_update_qrcode(uint8_t query_res, char * openid, char * access_token)
{
    lv_qq_obj_ext_t * ext = qq_get_ext();
    lv_obj_t * cont = ext->qr_bg_cont;
    if(cont == NULL) return;
    if(query_res == WATCH_QQ_REG_OK) {
        qq_show_login();
#if QQ_UI_TEST == 0
        my_access_token = lv_strdup(access_token);
        my_openid = lv_strdup(openid);
        //register req
        watch_qq_register_with_openid_req_t * login_info = lv_mem_alloc(sizeof(watch_qq_register_with_openid_req_t));
        memset(login_info, 0, sizeof(watch_qq_register_with_openid_req_t));
        memcpy(login_info->access_token, access_token, 64);
        memcpy(login_info->openid, openid, 64);
        login_info->state = 0;
        ext->login_state = 0;
        watch_qq_register_with_openid_req(login_info);
#endif
    } else if(query_res == WATCH_QQ_SCANED_UNREG) {
        lv_obj_t * label = lv_obj_get_child(cont, NULL);
        lv_label_set_text_id(label, WATCH_TEXT_ID_QQ_SCAN_SUCCESS);
        lv_obj_set_style_local_text_color(label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, QQ_COLOR_BLUE_NORMAL);
        lv_obj_realign(label);
        Hal_Timer_Start(MMI_QQ_QUERY_QR_TIME_INTERVAL, qq_query_qrcode, NULL, false);
    } else if(query_res == WATCH_QQ_UNSCANED) {
        Hal_Timer_Start(MMI_QQ_QUERY_QR_TIME_INTERVAL, qq_query_qrcode, NULL, false);
    } else {
        qq_show_qrcode_invalid();
    }
}

void qq_create_get_qr_failed(void)
{
    lv_qq_obj_ext_t * ext = qq_get_ext();
    lv_obj_t * cont = ext->qr_bg_cont;
    lv_obj_t * qr_bg = lv_obj_get_child(cont, lv_obj_get_child(cont, NULL));
    lv_obj_del(lv_obj_get_child(qr_bg, NULL));
    lv_obj_t * refresh_img = lv_img_create(qr_bg, NULL);
    lv_img_set_src(refresh_img, ICON_QQ_REFRESH2);
    lv_obj_align(refresh_img, NULL, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_click(refresh_img, true);
    lv_obj_set_event_cb(refresh_img, qq_invalid_qrcode_refresh_event_cb);

    lv_obj_t * label = lv_obj_get_child(cont, NULL);
    lv_label_set_text_id(label, WATCH_TEXT_ID_QQ_GET_QR_FAILED);
    lv_obj_set_style_local_text_color(label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED);
    lv_obj_realign(label);
    lv_obj_t * label_tip = lv_label_create(cont, NULL);
    lv_label_set_text_id(label_tip, WATCH_TEXT_ID_QQ_CLICK_REFRESH);
    lv_label_set_align(label_tip, LV_LABEL_ALIGN_CENTER);
    lv_obj_align(label_tip, label, LV_ALIGN_OUT_BOTTOM_MID, 0, 0);
}

void qq_logout_btn_event_cb(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;
    qq_create_msgbox(lv_obj_get_parent(btn), WATCH_TEXT_ID_QQ_QUIT_CONFIRM, qq_logout_confirm_event_cb);
}

void qq_create_no_network(lv_obj_t * par)
{
    lv_obj_t * cont = qq_create_bg_cont(par, LV_OPA_COVER, 0);
    lv_obj_t * img = lv_img_create(cont, NULL);
    lv_img_set_src(img, ICON_QQ_NO_NET);
    lv_obj_align(img, par, LV_ALIGN_CENTER, 0, - LV_VER_RES / 6);
    lv_obj_t * label = lv_label_create(cont, NULL);
    lv_label_set_text_id(label, WATCH_TEXT_ID_QQ_NO_NET);
    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
    lv_obj_set_style_local_text_color(label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, QQ_COLOR_TEXT_LIGHT);
    lv_obj_align(label, img, LV_ALIGN_OUT_BOTTOM_MID, 0, LV_VER_RES / 30);
    lv_obj_t * btn = lv_btn_create(cont, NULL);
    lv_obj_set_size(btn, LV_HOR_RES / 4, LV_VER_RES / 6);
    lv_obj_set_style_local_bg_color(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, QQ_COLOR_BLUE_NORMAL);
    lv_obj_set_style_local_bg_color(btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, QQ_COLOR_BLUE_PRESSED);
    lv_obj_set_style_local_radius(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 20);
    lv_obj_align(btn, cont, LV_ALIGN_IN_BOTTOM_RIGHT,  -LV_VER_RES / 30,  -LV_VER_RES / 30);
    lv_obj_t * btn_label = lv_label_create(btn, NULL);
    lv_label_set_text_id(btn_label, WATCH_TEXT_ID_QQ_QUIT);
    lv_obj_set_event_cb(btn, qq_no_net_btn_event_cb);
}

void qq_create_scan_login(void)
{
    lv_qq_obj_ext_t * ext = qq_get_ext();
    lv_img_set_src(ext->login, ICON_QQ_SCAN_LOGIN);

    lv_obj_t * btn = lv_btn_create(ext->login, NULL);
    lv_obj_set_size(btn, LV_HOR_RES * 5 / 6 - QQ_PAD_HOR * 2, LV_VER_RES / 5 - QQ_PAD_VER);
    lv_obj_set_style_local_bg_color(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, QQ_COLOR_BLUE_NORMAL);
    lv_obj_set_style_local_bg_color(btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, QQ_COLOR_BLUE_PRESSED);
    lv_obj_set_style_local_radius(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 50);
    lv_obj_set_event_cb(btn, qq_scan_login_btn_event_cb);
    lv_obj_t * label = lv_label_create(btn, NULL);
    lv_label_set_text_id(label, WATCH_TEXT_ID_QQ_SCAN_LOGIN);
    lv_obj_set_style_local_text_color(label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, QQ_COLOR_TEXT_DARK);
    lv_obj_align(btn, NULL, LV_ALIGN_IN_BOTTOM_MID, 0, -LV_VER_RES / 20 - QQ_PAD_VER);
}

void qq_create_switch_account(void)
{
    lv_obj_t * obj = qq_get_obj();

    watch_qq_login_info_t * reg_info = watch_qq_get_last_login_info();
    lv_obj_t * cont = qq_create_bg_cont(obj, LV_OPA_COVER, 0);
    lv_obj_t * small_cont = lv_cont_create(cont, NULL);
    lv_obj_set_size(small_cont, LV_HOR_RES * 7 / 8 - QQ_PAD_HOR * 2, LV_VER_RES / 2 - QQ_PAD_VER);
    lv_obj_set_style_local_bg_color(small_cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, QQ_COLOR_BG_GRAY);
    lv_obj_set_style_local_radius(small_cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 5);
    lv_obj_align(small_cont, cont, LV_ALIGN_CENTER, 0, 0);
    lv_obj_t * head_img = lv_img_create(cont, NULL);
    if(strlen(reg_info->head_path) != 0)
        lv_img_set_src(head_img, reg_info->head_path);
    else
        lv_img_set_src(head_img, ICON_QQ_DEF_HEAD_MID);
    lv_img_set_zoom(head_img, 348);
    lv_obj_align(head_img, cont, LV_ALIGN_IN_TOP_MID, 0, lv_obj_get_y(small_cont) - lv_obj_get_height(head_img) / 3);
    lv_obj_t * name_label = lv_label_create(cont, NULL);
    lv_label_set_text(name_label, reg_info->nickname);
    lv_label_set_long_mode(name_label, LV_LABEL_LONG_DOT);
    lv_obj_set_width(name_label, lv_obj_get_width(small_cont) * 2 / 3);
    lv_label_set_align(name_label, LV_LABEL_ALIGN_CENTER);
    lv_obj_align(name_label, head_img, LV_ALIGN_OUT_BOTTOM_MID, 0, lv_obj_get_height(head_img) / 4);
    lv_obj_t * btn = lv_btn_create(cont, NULL);
    lv_obj_set_size(btn, LV_HOR_RES / 3, LV_VER_RES / 6);
    lv_obj_set_style_local_bg_color(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, QQ_COLOR_BLUE_NORMAL);
    lv_obj_set_style_local_bg_color(btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, QQ_COLOR_BLUE_PRESSED);
    lv_obj_set_style_local_radius(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 50);
    lv_obj_set_event_cb(btn, qq_login_btn_event_cb);
    lv_watch_set_free_ptr(btn, (void *)reg_info);
    lv_obj_t * label = lv_label_create(btn, NULL);
    lv_label_set_text_id(label, WATCH_TEXT_ID_QQ_LOGIN);
    lv_obj_set_style_local_text_color(label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
    lv_obj_align(btn, name_label, LV_ALIGN_OUT_BOTTOM_MID, 0, lv_obj_get_height(name_label) / 4);

    lv_obj_t * switch_label = lv_label_create(cont, NULL);
    lv_obj_set_style_local_text_color(switch_label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
    lv_label_set_text_id(switch_label, WATCH_TEXT_ID_QQ_SWITCH_ACCOUNT);
    lv_obj_set_click(switch_label, true);
    lv_obj_align(switch_label, cont, LV_ALIGN_IN_BOTTOM_LEFT, QQ_PAD_HOR * 9 / 2, -lv_obj_get_height(switch_label) / 3 - QQ_PAD_VER);
    lv_obj_set_event_cb(switch_label, qq_switch_account_event_cb);

    lv_watch_obj_add_element(cont);
    lv_watch_obj_add_element(small_cont);
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static void qq_scan_login_btn_event_cb(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;
#if QQ_UI_TEST == 0
    watch_qq_get_qr_req();
#endif
    qq_create_qrcode_scan(qq_get_obj());
}

static void qq_no_net_btn_event_cb(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;
    lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_QQ_MAIN);
    lv_obj_del(activity_obj);
}

static void qq_invalid_qrcode_refresh_event_cb(lv_obj_t * img, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;
    lv_obj_t * qr_bg = lv_obj_get_parent(img);
    lv_obj_clean(qr_bg);
    lv_obj_t * preload = lv_spinner_create(qr_bg, NULL);
    lv_obj_set_size(preload, LV_HOR_RES / 10, LV_VER_RES / 10);
    lv_obj_set_style_local_line_color(preload, LV_SPINNER_PART_BG, LV_STATE_DEFAULT, LV_COLOR_WHITE);
    lv_obj_set_style_local_line_width(preload, LV_SPINNER_PART_BG, LV_STATE_DEFAULT, 2);
    lv_obj_set_style_local_line_rounded(preload, LV_SPINNER_PART_BG, LV_STATE_DEFAULT, 1);
    lv_obj_set_style_local_line_color(preload, LV_SPINNER_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_GRAY);
    lv_obj_set_style_local_line_width(preload, LV_SPINNER_PART_INDIC, LV_STATE_DEFAULT, 2);
    lv_spinner_set_type(preload, LV_SPINNER_TYPE_SPINNING_ARC);
    lv_spinner_set_spin_time(preload, 2000);
    lv_obj_align(preload, NULL, LV_ALIGN_CENTER, 0, 0);

    lv_obj_t * par_cont = lv_obj_get_parent(qr_bg);
    lv_obj_t * label_tip = lv_obj_get_child(par_cont, NULL);
    lv_obj_del(label_tip);
    lv_obj_t * label = lv_obj_get_child(par_cont, NULL);
    lv_obj_set_style_local_text_color(label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
    lv_label_set_text_id(label, WATCH_TEXT_ID_QQ_PHONE_SCAN);
    lv_obj_realign(label);

#if QQ_UI_TEST
    Hal_Timer_Start(1000, qq_show_qrcode_timer_cb, NULL, false);
#else
    watch_qq_get_qr_req();
#endif
}

static void qq_show_qrcode_invalid(void)
{
    lv_qq_obj_ext_t * ext = qq_get_ext();
    lv_obj_t * par = ext->qr_bg_cont;
    lv_obj_t * label = lv_obj_get_child(par, NULL);
    lv_label_set_text_id(label, WATCH_TEXT_ID_QQ_INVALID_QRCODE);
    lv_obj_set_style_local_text_color(label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED);
    lv_obj_realign(label);
    lv_obj_t * label_tip = lv_label_create(par, NULL);
    lv_label_set_text_id(label_tip, WATCH_TEXT_ID_QQ_CLICK_REFRESH);
    lv_label_set_align(label_tip, LV_LABEL_ALIGN_CENTER);
    lv_obj_align(label_tip, label, LV_ALIGN_OUT_BOTTOM_MID, 0, 0);

    lv_obj_t * qr_code_bg = lv_obj_get_child(par, label);
    lv_obj_t * refresh_img = lv_img_create(qr_code_bg, NULL);
    lv_img_set_src(refresh_img, ICON_QQ_REFRESH);
    lv_obj_align(refresh_img, NULL, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_click(refresh_img, true);
    lv_obj_set_event_cb(refresh_img, qq_invalid_qrcode_refresh_event_cb);
}

static void qq_create_qrcode_scan(lv_obj_t * par)
{
    lv_obj_t * cont = qq_create_bg_cont(par, LV_OPA_COVER, 0);
    lv_qq_obj_ext_t * ext = qq_get_ext();
    ext->qr_bg_cont = cont;
    qq_create_back_img(cont, QQ_QRCODE);
    lv_obj_t * qr_bg = lv_cont_create(cont, NULL);
    lv_obj_set_size(qr_bg, LV_HOR_RES / 2, LV_VER_RES / 2);
    lv_obj_align(qr_bg, cont, LV_ALIGN_IN_TOP_MID, 0, LV_VER_RES / 6);
    lv_obj_set_style_local_bg_color(qr_bg, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, QQ_COLOR_BLUE_NORMAL);
    lv_obj_set_style_local_radius(qr_bg, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 5);

    lv_obj_t * preload = lv_spinner_create(qr_bg, NULL);
    lv_obj_set_size(preload, LV_HOR_RES / 10, LV_VER_RES / 10);
    lv_obj_set_style_local_line_color(preload, LV_SPINNER_PART_BG, LV_STATE_DEFAULT, LV_COLOR_WHITE);
    lv_obj_set_style_local_line_width(preload, LV_SPINNER_PART_BG, LV_STATE_DEFAULT, 2);
    lv_obj_set_style_local_line_rounded(preload, LV_SPINNER_PART_BG, LV_STATE_DEFAULT, 1);
    lv_obj_set_style_local_line_color(preload, LV_SPINNER_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_GRAY);
    lv_obj_set_style_local_line_width(preload, LV_SPINNER_PART_INDIC, LV_STATE_DEFAULT, 2);
    lv_spinner_set_type(preload, LV_SPINNER_TYPE_SPINNING_ARC);
    lv_spinner_set_spin_time(preload, 2000);
    lv_obj_align(preload, NULL, LV_ALIGN_CENTER, 0, 0);

    lv_obj_t * label = lv_label_create(cont, NULL);
    lv_label_set_text_id(label, WATCH_TEXT_ID_QQ_PHONE_SCAN);
    lv_obj_set_style_local_text_color(label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
    lv_obj_align(label, qr_bg, LV_ALIGN_OUT_BOTTOM_MID, 0, LV_VER_RES / 20);
    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);

#if QQ_UI_TEST
    Hal_Timer_Start(1000, qq_show_qrcode_timer_cb, cont, false);
#endif
}

static void qq_logout_confirm_event_cb(lv_obj_t * btnm, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;
    char * txt = (char *)lv_btnmatrix_get_active_btn_text(btnm);
    if(strcmp(txt, (char *)lv_lang_get_text(WATCH_TEXT_ID_OK)) == 0) {
        lv_obj_t * obj = qq_get_obj();
        lv_qq_obj_ext_t * ext = lv_obj_get_ext_attr(obj);
        lv_obj_del(lv_obj_get_child(obj, NULL));
        ext->leds_content = NULL;
        ext->recentmsg = NULL;
        ext->friend = NULL;
        ext->mine = NULL;
        ext->recent_list = NULL;
        ext->new_msg_type = 0;
        qq_get_data_info_t * node = (qq_get_data_info_t *)_lv_ll_get_head(&ext->getdata_ll);
        while(node) {
            lv_mem_free(node->url);
            node = _lv_ll_get_next(&ext->getdata_ll, node);
        }
        _lv_ll_clear(&ext->getdata_ll);
        ext->login = lv_img_create(obj, NULL);
        lv_img_set_src(ext->login, ICON_QQ_LOGINING);
        lv_obj_t * label = lv_label_create(ext->login, NULL);
        lv_label_set_text_id(label, WATCH_TEXT_ID_QQ_LOGGING_OUT);
        lv_obj_set_style_local_text_color(label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
        lv_obj_align(label, NULL, LV_ALIGN_CENTER, 0, LV_VER_RES / 3);
#if QQ_UI_TEST
        Hal_Timer_Start(1000, qq_logging_out_timer_cb, NULL, false);
#else
        watch_qq_register_with_openid_req_t * reg_req =
            (watch_qq_register_with_openid_req_t *)lv_mem_alloc(sizeof(watch_qq_register_with_openid_req_t));
        watch_qq_login_info_t * reg_info = watch_qq_get_last_login_info();
        memcpy(reg_req->openid, reg_info->openid, 64);
        memcpy(reg_req->access_token, reg_info->access_token, 64);
        reg_req->state = 1;
        ext->login_state = 1;
        watch_qq_register_with_openid_req(reg_req);
        lv_mem_free(reg_info);
#endif
    } else {
        lv_obj_del(lv_obj_get_parent(lv_obj_get_parent(btnm)));
    }
}

static void qq_switch_account_event_cb(lv_obj_t * obj, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;
    lv_qq_obj_ext_t * ext = qq_get_ext();
    lv_obj_del(lv_obj_get_parent(obj));
    lv_obj_clean(ext->login);
    lv_img_set_src(ext->login, ICON_QQ_SCAN_LOGIN);
    lv_obj_t * btn = lv_btn_create(ext->login, NULL);
    lv_obj_set_size(btn, LV_HOR_RES * 5 / 6 - QQ_PAD_HOR * 2, LV_VER_RES / 5 - QQ_PAD_VER);
    lv_obj_set_style_local_bg_color(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, QQ_COLOR_BLUE_NORMAL);
    lv_obj_set_style_local_bg_color(btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, QQ_COLOR_BLUE_PRESSED);
    lv_obj_set_style_local_radius(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 50);
    lv_obj_set_event_cb(btn, qq_scan_login_btn_event_cb);
    lv_obj_t * label = lv_label_create(btn, NULL);
    lv_label_set_text_id(label, WATCH_TEXT_ID_QQ_SCAN_LOGIN);
    lv_obj_set_style_local_text_color(label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, QQ_COLOR_TEXT_DARK);
    lv_obj_align(btn, NULL, LV_ALIGN_IN_BOTTOM_MID, 0, -LV_VER_RES / 20 - QQ_PAD_VER);
}

static void qq_login_btn_event_cb(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED == event) {
#if QQ_UI_TEST == 0
        lv_qq_obj_ext_t * ext = qq_get_ext();
        watch_qq_register_with_openid_req_t * reg_req =
            (watch_qq_register_with_openid_req_t *)lv_mem_alloc(sizeof(watch_qq_register_with_openid_req_t));
        watch_qq_login_info_t * reg_info = lv_watch_get_free_ptr(btn);
        memcpy(reg_req->openid, reg_info->openid, 64);
        memcpy(reg_req->access_token, reg_info->access_token, 64);
        reg_req->state = 0;
        ext->login_state = 0;
        watch_qq_register_with_openid_req(reg_req);
#endif
        lv_obj_del(lv_obj_get_parent(btn));
        qq_show_login();
    } else if(LV_EVENT_DELETE == event) {
        watch_qq_login_info_t * reg_info = lv_watch_get_free_ptr(btn);
        lv_mem_free(reg_info);
    }
}
#endif /*USE_LV_WATCH_QQ*/
